/**
 * Created by zhangyilin on 2018/12/8.
 */


import java.sql.Connection;
import java.sql.DriverManager;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class task_seek {
    private int TaskID;//任务编号，数据库自增
    private String TaskTheme;//任务主题
    private String TaskIssuTime;//任务发布日期
    private String TaskDeadline;//任务截止日期
    private String UserID;//任务发布人ID,数据库中可考虑在表名中存放
    private String TaskGroup;//任务所属组,使用InviteID关联对应
    /*TaskContent属性*/
    private String TaskContent;//任务内容
    private String CompleteStatus;//完成度

    public Connection GetConnection() {//与数据库建立连接
        String url = "jdbc:mysql://localhost:3306/listsystem";//与本机的3306端口建立连接，进入数据库team
        String driverName = "com.mysql.jdbc.Driver";//驱动注册
        String userName = "root";
        String password = "";
        Connection conn = null;//用于建立与数据库的连接
        try{
            Class.forName(driverName);// jdbc4.0 后无需使用这句进行驱动注册操作
            conn = DriverManager.getConnection(url, userName, password);
        }catch(Exception e){
            System.out.println(e);
        }
        return conn;
    }

    String tip;
    public task_seek(int TaskID) {
        setTaskID(TaskID);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        //setTaskIssuTime(df.format(new Date()));//获得当前日期
        String time = df.format(new Date());
        System.out.println(df.format(new Date()));
        String[] times = time.split("-");
        int[] times1 = new int[3];
        for(int i=0;i<3;i++) {
            times1[i] = Integer.parseInt(times[i]);
        }

        Connection conn = GetConnection();

        conn = GetConnection();
        PreparedStatement getTaskconPS = null;
        ResultSet getTaskconRS = null;//查询获取任务内容
        String getTaskconSql = "select TaskContent from v_task_stu where TaskID=?";
        if(conn != null)
            try {
                getTaskconPS = conn.prepareStatement(getTaskconSql);
                if(getTaskconPS != null) {
                    getTaskconPS.setLong(1, getTaskID());
                    getTaskconRS = getTaskconPS.executeQuery();
                    if(getTaskconRS != null && getTaskconRS.next()) {
                        TaskContent = getTaskconRS.getString("TaskContent");
                        setTaskContent(TaskContent);
                    }

                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(getTaskconRS != null)
                        getTaskconRS.close();
                    if(getTaskconPS != null)
                        getTaskconPS.close();
                    if(conn != null)
                        conn.close();
                    getTaskconRS = null;
                    getTaskconPS = null;
                    conn = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }



        conn = GetConnection();
        PreparedStatement getTaskconPS1 = null;
        ResultSet getTaskconRS1 = null;//查询获取任务内容
        String getTaskconSql1 = "select TaskDeadline,TaskTheme from  v_task_stu where  TaskID=?";
        if(conn != null)
            try {
                getTaskconPS1 = conn.prepareStatement(getTaskconSql1);
                if(getTaskconPS1 != null) {
                    getTaskconPS1.setLong(1, getTaskID());
                    getTaskconRS1 = getTaskconPS1.executeQuery();
                    if(getTaskconRS1 != null && getTaskconRS1.next()) {
                        TaskDeadline = getTaskconRS1.getString("TaskDeadline");
                        setTaskDeadline(TaskDeadline);

                        TaskTheme = getTaskconRS1.getString("TaskTheme");
                        setTaskTheme(TaskTheme);
                    }
                    String[] times2 = TaskDeadline.split("-");
                    int[] times3 = new int[3];
                    for(int i=0;i<3;i++) {
                        times3[i] = Integer.parseInt(times2[i]);
                    }
                    if(times1[0]<times3[0]) {
                        tip = "进行中";
                    }

                    if(times1[0]>times3[0]) {
                        tip = "已截止";
                    }

                    if(times1[0]==times3[0]) {
                        if(times1[1]>times3[1]) {
                            tip = "已截止";
                        }

                        if(times1[1]<times3[1]) {
                            tip = "进行中";
                        }

                        if(times1[1]==times3[1]) {
                            if(times1[2]>times3[2]) {
                                tip = "已截止";
                            }

                            if(times1[2]<times3[2]) {
                                tip = "进行中";
                            }
                        }


                    }

                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(getTaskconRS1 != null)
                        getTaskconRS1.close();
                    if(getTaskconPS1 != null)
                        getTaskconPS1.close();
                    if(conn != null)
                        conn.close();
                    getTaskconRS1 = null;
                    getTaskconPS1 = null;
                    conn = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

    }





    public String getTaskContent() {
        return TaskContent;
    }
    public void setTaskContent(String taskContent) {
        TaskContent = taskContent;
    }
    public String getCompleteStatus() {
        return CompleteStatus;
    }
    public void setCompleteStatus(String completeStatus) {
        CompleteStatus = completeStatus;
    }
    public int getTaskID() {
        return TaskID;
    }
    public void setTaskID(int taskID) {
        TaskID = taskID;
    }
    public String getTaskTheme() {
        return TaskTheme;
    }
    public void setTaskTheme(String taskTheme) {
        TaskTheme = taskTheme;
    }
    public String getTaskIssuTime() {
        return TaskIssuTime;
    }
    public void setTaskIssuTime(String taskIssuTime) {
        TaskIssuTime = taskIssuTime;
    }
    public String getTaskDeadline() {
        return TaskDeadline;
    }
    public void setTaskDeadline(String taskDeadline) {
        TaskDeadline = taskDeadline;
    }
    public String getUserID() {
        return UserID;
    }
    public void setUserID(String userID) {
        UserID = userID;
    }
    public String getTaskGroup() {
        return TaskGroup;
    }
    public void setTaskGroup(String taskGroup) {
        TaskGroup = taskGroup;
    }

    @Override
    public String toString() {
        return tip;
    }
}

